%% Close all
close all;

%%
% eyelid contour script
I = imread('data\30.3.jpg');

%% Skin classifiers
[x a] = bc(I,normsh,nnsh);
x = x - 1;
y = zeros(size(x));

%% removing the noise
x = bwareaopen(x,100);

%% clear border
x = imclearborder(x,4);

%% fill the holes
x = imfill(x,'holes');

%% Perform connected component
CC = bwconncomp(x);
numPixels = cellfun(@numel,CC.PixelIdxList);
[biggest,idx] = max(numPixels);
y(CC.PixelIdxList{idx}) = 1;

figure(1); imshow(y);

%% Some processing
% smooth the edge
seD = strel('diamond',1);
y = imerode(y,seD);
y = imerode(y,seD);

figure(1); imshow(y);

%% Draw the outline
outline = bwperim(y);
r = I(:,:,1);
r(outline) = 255;
I(:,:,1) = r;
figure(2); imshow(I);